package com.zhuifeng.orders.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.zhuifeng.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuifeng.common.log.annotation.Log;
import com.zhuifeng.common.log.enums.BusinessType;
import com.zhuifeng.common.security.annotation.RequiresPermissions;
import com.zhuifeng.orders.domain.OrdersDetails;
import com.zhuifeng.orders.service.IOrdersDetailsService;
import com.zhuifeng.common.core.web.controller.BaseController;
import com.zhuifeng.common.core.web.domain.AjaxResult;
import com.zhuifeng.common.core.utils.poi.ExcelUtil;
import com.zhuifeng.common.core.web.page.TableDataInfo;

/**
 * 订单详情Controller
 * 
 * @author zhuifeng
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/details")
public class OrdersDetailsController extends BaseController
{
    @Autowired
    private IOrdersDetailsService ordersDetailsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping
    public R get(){
        // 通过用户ID和访问IP，处理重复请求
        String key = "123456";
        Object redisKey = redisTemplate.opsForValue().get(key);
        System.out.println("redis值：" + redisKey);

        // redis中的key值存在则表示当前这次请求距离上一次请求不超过下面设置的三秒钟，直接返回即可
        Long count = redisTemplate.opsForValue().increment(key, 1); // 设置递增因子
        System.out.println("递增的值" + count);
//        redisTemplate.expire(key, 3, TimeUnit.SECONDS); // 设置过期时间 3秒
        return R.ok("成功");

//        Map<String, Integer> map = new HashMap<>();
//        map.put("aa",5);
//        redisTemplate.boundHashOps("12345").put("aa",5);
//        redisTemplate.boundHashOps("12345").increment("aa",5);

    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("orders:details:list")
    @GetMapping("/list")
    public TableDataInfo list(OrdersDetails ordersDetails)
    {
        startPage();
        List<OrdersDetails> list = ordersDetailsService.selectOrdersDetailsList(ordersDetails);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @RequiresPermissions("orders:details:export")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrdersDetails ordersDetails)
    {
        List<OrdersDetails> list = ordersDetailsService.selectOrdersDetailsList(ordersDetails);
        ExcelUtil<OrdersDetails> util = new ExcelUtil<OrdersDetails>(OrdersDetails.class);
        util.exportExcel(response, list, "订单详情数据");
    }

    /**
     * 获取订单详情详细信息
     */
    @RequiresPermissions("orders:details:query")
    @GetMapping(value = "/{ordersDetailsId}")
    public AjaxResult getInfo(@PathVariable("ordersDetailsId") String ordersDetailsId)
    {
        return AjaxResult.success(ordersDetailsService.selectOrdersDetailsByOrdersDetailsId(ordersDetailsId));
    }

    /**
     * 新增订单详情
     */
    @RequiresPermissions("orders:details:add")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrdersDetails ordersDetails)
    {
        return toAjax(ordersDetailsService.insertOrdersDetails(ordersDetails));
    }

    /**
     * 修改订单详情
     */
    @RequiresPermissions("orders:details:edit")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrdersDetails ordersDetails)
    {
        return toAjax(ordersDetailsService.updateOrdersDetails(ordersDetails));
    }

    /**
     * 删除订单详情
     */
    @RequiresPermissions("orders:details:remove")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ordersDetailsIds}")
    public AjaxResult remove(@PathVariable String[] ordersDetailsIds)
    {
        return toAjax(ordersDetailsService.deleteOrdersDetailsByOrdersDetailsIds(ordersDetailsIds));
    }

    /**
     * 订单详情
     */
    @RequiresPermissions("orders:details:ordersDetailsSelectId")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @GetMapping("/ordersDetailsSelectId/{ordersId}")
    public R<OrdersDetails> ordersDetailsSelectId(@PathVariable("ordersId")String ordersId) throws ExecutionException, InterruptedException {
        OrdersDetails ordersDetails=this.ordersDetailsService.ordersDetailsId(ordersId);
        return R.ok(ordersDetails);
    }



}
